package jpa.beans;

import java.io.Serializable;
import java.util.Collection;
import javax.ejb.*;
import javax.persistence.*;

import jpa.entities.Product;
import jpa.interfaces.ProductBeanInterface;

@Remote(ProductBeanInterface.class)
@Stateful(mappedName = "ProductBeans")
public class ProductBean implements Serializable, ProductBeanInterface {
	private static final long serialVersionUID = -2875458375584064192L;

	@PersistenceContext
	private EntityManager em;

	@Override
	public void addProduct(String naam, String omschrijving, double dePrijs,
			String soort) {
		Product p;

		p = new Product();
		p.setProductNaam(naam);
		p.setProductOmschrijving(omschrijving);
		p.setProductPrijs(dePrijs);
		p.setProductSoort(soort);
		em.persist(p);
	}

	public Collection<Product> getAllProducts() {
		Query q = em.createQuery("SELECT p FROM Product p ORDER BY p.id",Product.class);

		System.out.println("Hij komt uit de for");
		Collection<Product> departments = q.getResultList();
		return departments;
	}

	public Product getProductById(int id) {
		Query q = em.createQuery("SELECT p FROM Product p where p.id = :deptid", Product.class);

		q.setParameter("deptid", id);
		System.out.println("Hij komt uit de for");
		
		return (Product) q.getSingleResult();
	}

	@Override
	public Collection<String> getAllProductSoorten() {
		// TODO Auto-generated method stub
		Query q = em.createQuery("SELECT p.productSoort FROM Product p GROUP BY p.productSoort",Product.class);
		Collection<String> soorten = q.getResultList();
		return soorten;
	}

	@Override
	public Collection<Product> getProductsBijSoort(String Soort) {
		// TODO Auto-generated method stub
		Query q = em.createQuery("SELECT p FROM Product p where p.productSoort = :deptid", Product.class);

		q.setParameter("deptid", Soort);

		System.out.println("Hij komt uit de for");
		Collection<Product> result = q.getResultList();
		return result;
	}
}
